miscellaneous cleanups & bug fixes.
authortsteven4 <tsteven4@gmail.com>
Wed, 4 Jul 2018 20:20:00 +0000 (14:20 -0600)
committertsteven4 <tsteven4@gmail.com>
Wed, 4 Jul 2018 20:20:00 +0000 (14:20 -0600)
easygps.cc: bugfix - clang-tidy check misc-sizeof-expression
garmin_fs.cc: warning cleanup - gcc warning: warning: ‘%d’ directive output may be truncated writing between 1 and 11 bytes into a region of size 3 [-Wformat-truncation=]
jtr.cc: warning cleanup - gcc warning: ‘__builtin___snprintf_chk’ output may be truncated before the last format character [-Wformat-truncation=]
jtr.cc: bugfix - the jtr writter could incorrecly print the msec portion of the creationtime by dropping the most significant digit when it was zero.
mapsource.cc: warning cleanup - binary operator acts on identical operands
random.cc: bugfix - fractional seconds were always zero.
tpo.cc: warning cleanup - gcc warning: argument 2 null where non-null expected [-Wnonnull]
xmlgeneric.cc: warning cleanup - Declarator is never used

easygps.cc
garmin_fs.cc
jtr.cc
mapsource.cc
random.cc
tpo.cc
xmlgeneric.cc

index 0fbb192266d24285a8554f3cd5675d728be2c9f2..616d6c602b3439869db4f0c9ef77d3b0a732444f 100644 (file)
@@ -48,7 +48,7 @@ rd_init(const QString& fname)
   sz = gbfread(ibuf, 1, 52, file_in);
 
   if ((sz < 52) ||
-      strncmp(ibuf, ezsig, sizeof(ezsig)-1) ||
+      strncmp(ibuf, ezsig, strlen(ezsig)) ||
       (ibuf[51] != 'W')) {
     fatal(MYNAME ": %s is not an EasyGPS file.\n", qPrintable(fname));
   }
index ef292dcb53cbbd78fae8c665b7ed821ba8a0b95a..bdef0361ee0505f5731b696e6cd9b005e0de33d1 100644 (file)
@@ -27,6 +27,7 @@
 #include "inifile.h"
 
 #include <QtCore/QXmlStreamWriter>
+#include <cassert>
 #include <cstdio>
 #include <cstdlib>
 
@@ -388,6 +389,9 @@ garmin_fs_convert_category(const char* category_name, uint16_t* category)
       char* c;
       char key[3];
 
+      // use assertion to silence gcc 7.3 warning
+      // warning: ‘%d’ directive output may be truncated writing between 1 and 11 bytes into a region of size 3 [-Wformat-truncation=]
+      assert((i>=0) && (i<16));
       snprintf(key, sizeof(key), "%d", i + 1);
       c = inifile_readstr(global_opts.inifile, GMSD_SECTION_CATEGORIES, key);
       if ((c != nullptr) && (case_ignore_strcmp(c, category_name) == 0)) {
diff --git a/jtr.cc b/jtr.cc
index f4b7ad910ea0e71dd9e4b4b72766f248d6db969d..d85ede24d5be3c30b32dadc5172cd8bf26ecb3a1 100644 (file)
--- a/jtr.cc
+++ b/jtr.cc
@@ -22,6 +22,7 @@
 #include "defs.h"
 #include "csv_util.h"
 #include <QtCore/QHash>
+//#include <cassert>
 #include <cmath>
 #include <cstdio>
 #include <cstdlib>
@@ -265,23 +266,24 @@ static void
 jtr_trkpt_disp_cb(const Waypoint* wpt)
 {
   char* str, *tmp;
-  char stime[10], sdate[7], scourse[6], sspeed[8];
-  struct tm tm;
+  char scourse[6], sspeed[8];
+  QString sdate;
+  QString stime;
 
   if (wpt->creation_time.isValid()) {
-    const time_t tt = wpt->GetCreationTime().toTime_t();
-    tm = *gmtime(&tt);
-
-    tm.tm_year += 1900;
-    tm.tm_mon += 1;
-    snprintf(sdate, sizeof(sdate), "%02d%02d%02d", tm.tm_mday, tm.tm_mon, tm.tm_year % 100);
-    snprintf(stime, sizeof(stime), "%02d%02d%02d.%02d", tm.tm_hour, tm.tm_min, tm.tm_sec, wpt->creation_time.time().msec());
-    if (wpt->creation_time.time().msec() == 0) {
-      stime[6] = 0;
-    }
-  } else {
-    stime[0] = 0;
-    sdate[0] = 0;
+    gpsbabel::DateTime dt = wpt->GetCreationTime().toUTC();
+
+    // round time to centiseconds.
+    int msec = dt.time().msec();
+    int csec = lround(msec/10.0);
+    dt = dt.addMSecs(csec*10-msec);
+    sdate = dt.toString(QStringLiteral("ddMMyy"));
+    stime = dt.toString(QStringLiteral("HHmmss.zzz"));
+    // toss milliseconds position which should now be zero.
+    //assert(stime.endsWith('0'));
+    stime.chop(1);
+    // suppress fractional seconds if they are zero.
+    stime = stime.replace(QLatin1String(".00"), QLatin1String(""));
   }
   if (WAYPT_HAS(wpt, speed) && (wpt->speed >= 0)) {
     snprintf(sspeed, sizeof(sspeed), "%.1f", MPS_TO_KNOTS(wpt->speed));
@@ -295,7 +297,7 @@ jtr_trkpt_disp_cb(const Waypoint* wpt)
   }
 
   xasprintf(&str, "GEOTAG2,%s,%c,%09.4f,%c,%010.4f,%c,%s,%s,%s,,E,A",
-            stime,
+            CSTR(stime),
             wpt->creation_time.isValid() ? 'A' : 'V',
             fabs(degrees2ddmm(wpt->latitude)),
             wpt->latitude < 0 ? 'S' : 'N',
@@ -303,7 +305,7 @@ jtr_trkpt_disp_cb(const Waypoint* wpt)
             wpt->longitude < 0 ? 'W' : 'E',
             sspeed,
             scourse,
-            sdate);
+            CSTR(sdate));
 
   xasprintf(&tmp, "%s*%02X", str, nmea_cksum(str));
   xfree(str);
index 916046cad0bc005c9d7bca815dfa90b7271374ee..17a3e59c14d80d3eed4c2ce8b6486220b485f49f 100644 (file)
@@ -609,7 +609,7 @@ mps_waypoint_r(gbfile* mps_file, int mps_ver, Waypoint** wpt, unsigned int* mpsc
  * MRCB
  */
 static void
-mps_waypoint_w(gbfile* mps_file, int mps_ver, const Waypoint* wpt, const int isRouteWpt)
+mps_waypoint_w(gbfile* mps_file, int mps_ver, const Waypoint* wpt, const bool isRouteWpt)
 {
   int reclen;
   int lat, lon;
@@ -768,7 +768,7 @@ mps_waypoint_w_unique_wrapper(const Waypoint* wpt)
 
   /* if this waypoint hasn't been written then it is okay to do so */
   if (wptfound == nullptr) {
-    mps_waypoint_w(mps_file_out, mps_ver_out, wpt, (1==0));
+    mps_waypoint_w(mps_file_out, mps_ver_out, wpt, false);
 
     /* ensure we record in our "private" queue what has been
     written so that we don't write it again */
@@ -807,10 +807,10 @@ mps_route_wpt_w_unique_wrapper(const Waypoint* wpt)
 
     if (wptfound == nullptr) {
       /* well, we tried to find: it wasn't written and isn't a real waypoint */
-      mps_waypoint_w(mps_file_out, mps_ver_out, wpt, (1==1));
+      mps_waypoint_w(mps_file_out, mps_ver_out, wpt, true);
       mps_wpt_q_add(&written_route_wpt_head, wpt);
     } else {
-      mps_waypoint_w(mps_file_out, mps_ver_out, wpt, (1==0));
+      mps_waypoint_w(mps_file_out, mps_ver_out, wpt, false);
       /* Simulated real user waypoint */
       mps_wpt_q_add(&written_wpt_head, wpt);
     }
@@ -849,11 +849,11 @@ mps_waypoint_w_uniqloc_wrapper(Waypoint* wpt)
       wptfound = new Waypoint(*wpt);
       xfree(wptfound->shortname);
       wptfound->shortname = newName;
-      mps_waypoint_w(mps_file_out, mps_ver_out, wptfound, (1==0));
+      mps_waypoint_w(mps_file_out, mps_ver_out, wptfound, false);
       mps_wpt_q_add(&written_wpt_head, wpt);
     }
   } else {
-    mps_waypoint_w(mps_file_out, mps_ver_out, wpt, (1==0));
+    mps_waypoint_w(mps_file_out, mps_ver_out, wpt, false);
     /* ensure we record in out "private" queue what has been
     written so that we don't write it again */
     mps_wpt_q_add(&written_wpt_head, wpt);
index 89631f4dc84fbe7d4c2056ccb4edab4361f43a96..d30fde92fe07d0b4f2e196b3753512801b644369 100644 (file)
--- a/random.cc
+++ b/random.cc
@@ -183,7 +183,7 @@ random_read()
 
     wpt->SetCreationTime(time);
     if RND(3) {
-      wpt->creation_time = wpt->creation_time.addMSecs(rand_int(1000) * 1000);
+      wpt->creation_time = wpt->creation_time.addMSecs(rand_int(1000));
     }
     time += rand_int(10) + 1;
 
diff --git a/tpo.cc b/tpo.cc
index d1036e296de21ceefa5aed009fb4a3c476b14246..7acd43b951d6ce40618f428b69d6cc7f81b5d00e 100644 (file)
--- a/tpo.cc
+++ b/tpo.cc
@@ -74,6 +74,7 @@
 #include "defs.h"
 #include "jeeps/gpsmath.h" /* for datum conversions */
 #include <QtCore/QScopedArrayPointer> // Wish we could use c++11...
+#include <cassert>
 #include <cmath>
 #include <cstdio>
 #include <cstdlib>
@@ -1453,6 +1454,10 @@ tpo_read()
 static void
 tpo_write_file_header()
 {
+  // this assertion will quiet gcc 7.3 warnings about output_state in the strncmp calls
+  // warning: argument 2 null where non-null expected [-Wnonnull]
+  assert(output_state != nullptr);
+
   /* force upper-case state name */
   strupper(output_state);
 
index aee85d1d2763d1def36afee560d9dea852d004f7..3b61370d34099d1ea616689fc3f3e4d87b0cb43a 100644 (file)
@@ -35,7 +35,6 @@
 #include <QtCore/QDebug>
 #endif
 
-static QString current_tag;
 static xg_tag_mapping* xg_tag_tbl;
 static QSet<QString> xg_ignore_taglist;
 
@@ -100,9 +99,10 @@ xml_consider_ignoring(const QStringRef& name)
 }
 
 static void
-xml_run_parser(QXmlStreamReader& reader, QString& current_tag)
+xml_run_parser(QXmlStreamReader& reader)
 {
   xg_callback* cb;
+  QString current_tag;
 
   while (!reader.atEnd()) {
     switch (reader.tokenType()) {
@@ -175,13 +175,12 @@ readnext:
 void xml_read()
 {
   gpsbabel::File file(rd_fname);
-  QString current_tag;
 
   file.open(QIODevice::ReadOnly);
 
   QXmlStreamReader reader(&file);
 
-  xml_run_parser(reader, current_tag);
+  xml_run_parser(reader);
   if (reader.hasError())  {
     fatal(MYNAME ":Read error: %s (%s, line %ld, col %ld)\n",
           qPrintable(reader.errorString()),
@@ -209,13 +208,11 @@ void xml_readprefixstring(const char* str)
 // determine file encoding, falls back to UTF-8 if unspecified.
 void xml_readstring(const char* str)
 {
-  QString current_tag;
-
   reader_data.append(str);
 
   QXmlStreamReader reader(reader_data);
 
-  xml_run_parser(reader, current_tag);
+  xml_run_parser(reader);
   if (reader.hasError())  {
     fatal(MYNAME ":Read error: %s (%s, line %ld, col %ld)\n",
           qPrintable(reader.errorString()),
@@ -229,10 +226,9 @@ void xml_readstring(const char* str)
 // encoding because the source is already Qt's internal UTF-16.
 void xml_readunicode(const QString& str)
 {
-  QString current_tag;
   QXmlStreamReader reader(str);
 
-  xml_run_parser(reader, current_tag);
+  xml_run_parser(reader);
 }
 
 /******************************************/